package com.xzd.utils;

import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts({
		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
				RowBounds.class, ResultHandler.class })
	})
public class MyBatisInterceptor implements Interceptor {

	private Integer value;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		System.out.println(invocation.getMethod().getName());
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		System.out.println(value);
		// Plugin类是插件的核心类，用于给target创建一个JDK的动态代理对象，触发intercept()方法
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
		value = Integer.valueOf((String) properties.get("value"));
	}

}